package com.mia.props.events;

import com.mia.craftstudio.minecraft.client.CSClientModelWrapperVBO;
import com.mia.props.DecoModelMetadata;
import com.mia.props.Props;
import com.mia.props.client.container.GuiDecobench;
import com.mia.props.common.ItemProps;
import com.mia.props.common.entities.TileDecobench;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent;
import java.util.Iterator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.StatCollector;
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
import net.minecraftforge.client.event.DrawBlockHighlightEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.oredict.OreDictionary;

public enum ClientEventHandler {
   INSTANCE;

   private int playerOrientation;
   private boolean canPlace;
   private CSClientModelWrapperVBO model = null;
   private int tx;
   private int ty;
   private int tz;
   private int tick = 0;

   public static void register() {
      MinecraftForge.EVENT_BUS.register(INSTANCE);
      FMLCommonHandler.instance().bus().register(INSTANCE);
   }

   @SubscribeEvent
   public void onItemTooltip(ItemTooltipEvent event) {
      if (event.itemStack.getItem().equals(Props.itemProps) && event.itemStack.hasTagCompound() && event.itemStack.getTagCompound().hasKey("savedProp")) {
         event.toolTip.add(EnumChatFormatting.RESET + "" + EnumChatFormatting.AQUA + StatCollector.translateToLocal("text.props.tooltip.saved_item"));
      }

      if (Minecraft.getMinecraft().currentScreen instanceof GuiDecobench) {
         if (((GuiDecobench)Minecraft.getMinecraft().currentScreen).isMouseOverButtonScrollArea()) {
            if (GuiScreen.isShiftKeyDown()) {
               int[] resources = TileDecobench.resourcesProvidedByStack(event.itemStack);

               for(int i = 0; i < resources.length; ++i) {
                  if (resources[i] != 0) {
                     event.toolTip.add(EnumChatFormatting.RESET + "" + EnumChatFormatting.GOLD + StatCollector.translateToLocal("text.props.tooltip.resource_cost." + i) + EnumChatFormatting.YELLOW + " " + resources[i]);
                  }
               }
            } else {
               event.toolTip.add(EnumChatFormatting.DARK_GRAY.toString() + EnumChatFormatting.ITALIC + StatCollector.translateToLocal("text.props.gui.info"));
            }
         } else {
            Iterator var6 = Props.allowableResourceItems.iterator();

            while(var6.hasNext()) {
               ItemStack allowedStack = (ItemStack)var6.next();
               if (OreDictionary.itemMatches(allowedStack, event.itemStack, false)) {
                  if (GuiScreen.isShiftKeyDown()) {
                     int[] resources = TileDecobench.resourcesProvidedByStack(event.itemStack);

                     for(int i = 0; i < resources.length; ++i) {
                        if (resources[i] != 0) {
                           event.toolTip.add(EnumChatFormatting.RESET + "" + EnumChatFormatting.GOLD + StatCollector.translateToLocal("text.props.tooltip.resources_provided." + i) + EnumChatFormatting.YELLOW + " " + resources[i]);
                        }
                     }

                     return;
                  } else {
                     event.toolTip.add(EnumChatFormatting.DARK_GRAY.toString() + EnumChatFormatting.ITALIC + StatCollector.translateToLocal("text.props.gui.info"));
                     break;
                  }
               }
            }
         }
      }

   }

   @SubscribeEvent
   public void onClientTick(ClientTickEvent event) {
      if (this.tick++ % 4 == 0) {
         this.model = null;
         EntityPlayer player = Minecraft.getMinecraft().thePlayer;
         if (player == null) {
            return;
         }

         ItemStack currentItem = player.inventory.getCurrentItem();
         if (currentItem == null) {
            return;
         }

         if (!(currentItem.getItem() instanceof ItemProps)) {
            return;
         }

         MovingObjectPosition target = Minecraft.getMinecraft().objectMouseOver;
         if (target == null) {
            return;
         }

         if (target.typeOfHit != MovingObjectType.BLOCK) {
            return;
         }

         int meta = currentItem.getItemDamage();
         DecoModelMetadata data = (DecoModelMetadata)Props.modelData.get(meta);
         this.playerOrientation = 0;
         if (!data.limitRotation) {
            this.playerOrientation = MathHelper.floor_double((double)(player.rotationYaw * 16.0F / 360.0F) + 0.5D) & 15;
         } else {
            int direction = MathHelper.floor_double((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
            this.playerOrientation = direction * 4;
         }

         ForgeDirection fg = ForgeDirection.VALID_DIRECTIONS[target.sideHit];
         this.tx = target.blockX + fg.offsetX;
         this.ty = target.blockY + fg.offsetY;
         this.tz = target.blockZ + fg.offsetZ;
         this.canPlace = ItemProps.canPlace(data.wrapper, player.worldObj, this.tx, this.ty, this.tz, player, this.playerOrientation);
         this.model = (CSClientModelWrapperVBO)((DecoModelMetadata)Props.modelData.get(meta)).wrapper;
      }

   }

   @SubscribeEvent
   public void onDrawBlockHighlight(DrawBlockHighlightEvent event) {
      if (this.model != null) {
         this.model.renderPlacement(event.player, event.partialTicks, this.canPlace, this.tx, this.ty, this.tz, this.playerOrientation);
      }

   }
}
